package GUI;

import 数据库和网络.myDatebase;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

public class answerSheet extends JFrame {
    static JPanel mainPanel;
    static JButton startButton;
    static JLabel timeLabel;
    JButton button;
    int total;
    static ArrayList<JButton> buttons;
    public static int selectionRow, definitionRow, fillingRow, subjectiveRow, selectionQuestionNum, definitionQuestionNum, fillingBlankQuestionNum, subjectiveQuestionNum;
    public static butThread thread = new butThread();

    public answerSheet(int total, int questionNum, int selectionQuestionNum, int definitionQuestionNum,
                       int fillingBlankQuestionNum, int subjectiveQuestionNum) {

        this.selectionQuestionNum = selectionQuestionNum;
        this.definitionQuestionNum = definitionQuestionNum;
        this.fillingBlankQuestionNum = fillingBlankQuestionNum;
        this.subjectiveQuestionNum = subjectiveQuestionNum;
        this.total = total;

        int index = 1;
        this.selectionRow = selectionQuestionNum / 4 + 1;
        this.definitionRow = definitionQuestionNum / 4 + 1;
        this.fillingRow = definitionQuestionNum / 4 + 1;
        this.subjectiveRow = subjectiveQuestionNum / 4 + 1;

        JLabel selectionQuestionLabel = new JLabel("选择题 共" + selectionQuestionNum + "道");
        JLabel definitionQuestionLabel = new JLabel("判断题 共" + definitionQuestionNum + "道");
        JLabel fillingBlankQuestionLabel = new JLabel("填空题 共" + fillingBlankQuestionNum + "道");
        JLabel subjectiveQuestionLabel = new JLabel("简答题 共" + subjectiveQuestionNum + "道");


        timeLabel = new JLabel();
        timeLabel.setBackground(Color.yellow);
        //timeLabel.setForeground(Color.RED);
        timeLabel.setFont(new Font("宋体", Font.PLAIN, 14));
        timeLabel.setHorizontalAlignment(SwingConstants.CENTER);


        mainPanel = new JPanel();
        mainPanel.setLayout(null);

        /*startButton.setBounds(125,30,100,30);*/
        timeLabel.setBounds(73, 70, 200, 30);


        mainPanel.add(timeLabel);

        buttons = new ArrayList<>();
        buttons.add(new JButton());

        selectionQuestionLabel.setBounds(50, 100, 100, 30);
        mainPanel.add(selectionQuestionLabel);


        definitionQuestionLabel.setBounds(50, 200 + (selectionRow - 1) * 40, 100, 30);
        mainPanel.add(definitionQuestionLabel);

        fillingBlankQuestionLabel.setBounds(50, 300 + (selectionRow - 1) * 40 + (definitionRow - 1) * 40, 100, 30);
        mainPanel.add(fillingBlankQuestionLabel);

        subjectiveQuestionLabel.setBounds(50, 400 + (selectionRow - 1) * 40 + (definitionRow - 1) * 40 + (fillingRow - 1) * 40, 100, 30);
        mainPanel.add(subjectiveQuestionLabel);

        this.setTitle("答题卡");
        this.setContentPane(mainPanel);
        this.setSize(350, 750);
        mainPanel.setSize(this.getSize());
        this.setLocation(931, 20);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

        thread = new butThread();
        thread.total = total;
        thread.start();

    }

    /*public class startListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面
            startButton.setEnabled(false);
            thread = new butThread();
            thread.total = total;
            thread.start();
        }
    }*/

    public void setButton(JButton button) {
        button.setSize(50, 50);
        button.setFont(new Font("宋体", Font.BOLD, 14));
        button.setBackground(Color.lightGray);
    }

}
class butThread extends Thread
{
    int time,hour,minute,second,total;


    public void dispose(){
    }

    private static void run2() {
        test.demo.dispose();
        answerSheet.timeLabel.setText("考试结束，请交卷");
        answerSheet.startButton.setEnabled(true);
        for(int i=1;i<=test.currentpaper.getQuestionNumber();i++){
            answerSheet.buttons.get(i).setEnabled(false);
        }
    }

    @Override
    public void run()
    {

        time=(int)(total/60);
        hour=total/3600;
        minute=(total-hour*3600)/60;
        second=total-hour*3600-minute*60;
        time*=60;
        while(total>0)
        {
            //更新界面，显示倒计时
            final String text=String.valueOf(hour+":"+minute+":"+second);

            SwingUtilities.invokeLater(()->{
                answerSheet.timeLabel.setText(text);
            });
            second--;
            total--;
            if(second<0){
                minute--;
                second=59;
            }
            if (minute<0){
                hour--;
                minute=59;
            }
            try {
                int t=total;
                myDatebase tet=new myDatebase("result_record");
                String sql=String.format( "update result_record.%s set remainTime='"+t+"' where studentID='"+enterWindow.currentStudent.getSchoolID()+"'",test.currentpaper.getName());
                tet.statement.execute(sql);
                tet.close();


                Thread.sleep(1000*1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }catch(SQLException e){
                e.printStackTrace();
            }
        }
        int a,b,aScore,bScore;
        a=0;
        aScore=0;
        b=0;
        bScore=0;
        for(int i=0;i<test.selectionQuestionNum;i++){
            if(test.resultSet[1][i].equals(test.currentpaper.questionSet.get(i).getAnswer())){
                a++;
                aScore+=test.currentpaper.questionSet.get(i).getFullScore();
            }
        }

        for(int i=test.selectionQuestionNum;i<test.definitionQuestionNum+test.selectionQuestionNum;i++){
            if(test.resultSet[1][i].equals(test.currentpaper.questionSet.get(i).getAnswer())){
                b++;
                bScore+=test.currentpaper.questionSet.get(i).getFullScore();
            }
        }

        scoreWindow jumpingScore=new scoreWindow(a,b,test.selectionQuestionNum,test.definitionQuestionNum,aScore,bScore);
        jumpingScore.setVisible(true);

        try {
            String name=test.currentpaper.getName();
            myDatebase saveDatebase=new myDatebase("result_record");
            for(int i=1;i<=test.currentpaper.getQuestionNumber();i++){

                String sql=String.format("update %s SET %s_current_answer='"+test.resultSet[1][i-1]
                                +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name,
                        test.resultSet[0][i-1]);
                saveDatebase.statement.execute(sql);
                sql=String.format("update %s SET remainTime=0 where studentID='"+
                        enterWindow.currentStudent.getSchoolID()+"'",name);
                saveDatebase.statement.execute(sql);
                sql=String.format("update %s SET %s_proper_answer='"+test.questionSet.get(i-1).getAnswer()
                                +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name,
                        test.resultSet[0][i-1]);
                saveDatebase.statement.execute(sql);
            }
            String sql=String.format("update %s SET selection_score='"+aScore
                    +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name);
            saveDatebase.statement.execute(sql);
            sql=String.format("update %s SET definition_score='"+bScore
                    +"'where studentID='"+ enterWindow.currentStudent.getSchoolID()+"'",name);
            saveDatebase.statement.execute(sql);
            saveDatebase.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        test.ansboard.thread.dispose();
        test.ansboard.dispose();
        test.demo.dispose();
        answerSheet.thread.stop();


        test.mainPanel.updateUI();






        //结束后更新界面
        SwingUtilities.invokeLater(butThread::run2);
    }

}